import moment from 'moment';
import qs from 'qs';
import { history } from 'umi';


/**
 * json转url参数
 */
export function parseParam(param, obj) {
  if (!obj || !param) return param;
  var paramurl = param;
  var keys = Object.keys(obj);
  if (keys.length != 0) paramurl += '?' + qs.stringify(obj);
  return paramurl;
}

/**
 * 获取URL路由参数
 */
export function getUrlParams() {
  var dataStr = window.location.search.substr(1);
  return qs.parse(dataStr);
}

/**
 * 路由跳转需要携带参数
 * path：路径地址
 * query：路径参数
 */
export function getUrl(params = {}) {
  const { query } = history.location;
  let url = params.path;
  let queryParams = {
    workspaceId: query?.workspaceId,
    workspaceName: query?.workspaceName,
    ...params.query,
  };
  if (params.path) {
    url = `${url}?${qs.stringify(queryParams)}`;
  }
  return url;
}
